<?php
namespace app\controllers;


use app\common\corelib\LocalUpload;
use app\logic\OrderLogic;

class IndexController extends BackendController
{

    function diffBetweenTwoDays ($day1, $day2)
    {
        $second1 = strtotime($day1);
        $second2 = strtotime($day2);

        if ($second1 < $second2) {
            $tmp = $second2;
            $second2 = $second1;
            $second1 = $tmp;
        }
        return ($second1 - $second2) / 86400;
    }

	public function actionIndex()
	{
	    $orderLogic = new OrderLogic();
        $today_start_time = date("Y-m-d 00:00:00");
        $today_end_time = date("Y-m-d H:i:s");
	    $today = $orderLogic->getOrderData($today_start_time,$today_end_time);
        if(!\Yii::$app->request->post()){
            $this_month_start = date("Y-m-01 00:00:00");
            //$day = $this->diffBetweenTwoDays(date("Y-m-01"),date("Y-m-d"));
            $day = date('t', strtotime(date("Y-m")));
            $order_data_chart = [];
            //var_dump($orderLogic->getOrderDataChart('2018-07-01 00:00:00','2018-07-01 23:59:59'));exit;
            for($i=0;$i<$day;$i++){
                $day_start_time = date("Y-m-d 00:00:00",strtotime(date("Y-m-d",strtotime("$this_month_start +$i day"))));
                $day_end_time = date("Y-m-d 23:59:59",strtotime(date("Y-m-d",strtotime("$this_month_start +$i day"))));

                $order_data_chart[] = $orderLogic->getOrderDataChart($day_start_time,$day_end_time);

            }

            return $this->render('index', [
                'today'=>$today,
                'order_data_chart'=>json_encode($order_data_chart),
                'theme' => '/assets/',
                'actionUrl' => U('index/index')
            ]);
        }else {
            $start_time = \Yii::$app->request->post('start_time');
            $end_time = \Yii::$app->request->post('end_time');
            $type = \Yii::$app->request->post('type');
            $time = \Yii::$app->request->post('time');

            if($start_time && $end_time){
                $start_time = $start_time ." 00:00:00";
                $end_time = $end_time ." 23:59:59";
                $day_order_data = $orderLogic->getOrderData($start_time,$end_time);
                echo json_encode($day_order_data);exit;
            }

            $order_data_chart = [];
            if($type==1){
                $first_date =date('Y-m-01', strtotime($time));
                $day = date('t', strtotime($time));
                for($i=0;$i<$day;$i++){
                    $day_start_time = date("Y-m-d 00:00:00",strtotime(date("Y-m-d",strtotime("$first_date +$i day"))));
                    $day_end_time = date("Y-m-d 23:59:59",strtotime(date("Y-m-d",strtotime("$first_date +$i day"))));
                    $order_data_chart[] = $orderLogic->getOrderDataChart($day_start_time,$day_end_time);
                }
            }else{
                $first_date =date('Y-01-01', strtotime($time));
                for($i=1;$i<=12;$i++){
                    $day_start_time = date("Y-m-01 00:00:00",strtotime(date("Y-m-d",strtotime("$first_date +$i month -1 day"))));
                    $day_end_time = date("Y-m-d 23:59:59",strtotime(date("Y-m-d",strtotime("$first_date +$i month -1 day"))));
                    $order_data_chart[] = $orderLogic->getOrderDataChart($day_start_time,$day_end_time,true);
                }
            }
            echo json_encode($order_data_chart);exit;
            /*return $this->render('index', [
                'start_time'=>$start_time,
                'end_time'=>$end_time,
                'type'=>$type,
                'time'=>$time,
                'today'=>$today,
                'day_order_data'=>$day_order_data,
                'order_data_chart'=>$order_data_chart,
                'theme' => '/assets/',
                'actionUrl' => U('index/index')
            ]);*/
        }
	}

    public function actionError()
	{
        return $this->render('error');
	}

    public function actionUpload(){
        $data = [ 'state'=>'FAIL','data'=>[] ];
        if(isset($_FILES['upfile']['tmp_name']) && !empty($_FILES['upfile']['tmp_name'])){
            LocalUpload::go_upload($_FILES['upfile']);
            $thumbs = LocalUpload::thumbs(100,100);
            $data['state'] = 'SUCCESS';
            $data['url'] = \Yii::$app->params['upload']['upload_domain'].'/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
            /*$tmp_name = $_FILES['upfile']['tmp_name'];
            $filename = mt_rand(10000,99999).$_FILES['upfile']['name'];
            $type     = $_FILES['upfile']['type'];
            $img_64   = 'data:'.$type.';base64,';
            $fileStr  = @file_get_contents($tmp_name);
            if($fileStr){
                $imsg_str = $img_64 . base64_encode($fileStr);
                $AliyunImage = new AliyunImage();
                $AliyunImage->uploadImg($imsg_str,$filename);
                $res = $AliyunImage->result();
                if($res['result']){
                    $data['state'] = 'SUCCESS';
                    $data['url'] = 'http://'.$res['imageUrl'];
                }
            }*/
        }
        echo json_encode($data);exit;
    }

    public function actionUploads(){
        $data = [ 'state'=>'FAIL' ];
        if(isset($_FILES['file']['tmp_name']) && !empty($_FILES['file']['tmp_name'])){
            LocalUpload::go_upload($_FILES['file']);
            $thumbs = LocalUpload::thumbs(100,100);
            $data['state'] = 'SUCCESS';
            $data['url'] = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
            /*$tmp_name = $_FILES['upfile']['tmp_name'];
            $filename = mt_rand(10000,99999).$_FILES['upfile']['name'];
            $type     = $_FILES['upfile']['type'];
            $img_64   = 'data:'.$type.';base64,';
            $fileStr  = @file_get_contents($tmp_name);
            if($fileStr){
                $imsg_str = $img_64 . base64_encode($fileStr);
                $AliyunImage = new AliyunImage();
                $AliyunImage->uploadImg($imsg_str,$filename);
                $res = $AliyunImage->result();
                if($res['result']){
                    $data['state'] = 'SUCCESS';
                    $data['url'] = 'http://'.$res['imageUrl'];
                }
            }*/
        }
        echo json_encode($data);exit;
    }

}
